package com.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectionTest {
    public static void main(String[] args) {

        try {
//            Class stdClass = Student.class;
//            System.out.println(stdClass.getField("score"));
//            System.out.println(stdClass.getField("name"));
//            System.out.println(stdClass.getFields());
//            System.out.println(stdClass.getDeclaredField("grade"));

//            Object p = new Person("Xiao Ming");
//            Class c = p.getClass();
//            Field f = c.getDeclaredField("name");
//            f.setAccessible(true);
//            Object value = f.get(p);
//            System.out.println(value.toString());

//            Person p = new Person("Xiao Ming");
//            Class c = p.getClass();
//            Field f = c.getDeclaredField("name");
//            f.setAccessible(true);
//            f.set(p, "Xiao Hong");
//            System.out.println(p.getName());



//            Class stdClass = Student.class;
//            System.out.println(stdClass.getMethod("getScore"));
//            System.out.println(stdClass.getMethod("getName"));
//            System.out.println(stdClass.getDeclaredMethod("getGrade", int.class));


//            String s = "Hello world!";
//            Method m = String.class.getMethod("substring", int.class, int.class);
//            String r = (String) m.invoke(s, 2, 5);
//            System.out.println(r);

//            Method m = Integer.class.getMethod("parseInt", String.class);
//            Integer n = (Integer) m.invoke(null, "12345");
//            System.out.println(n);

//            Person p = new Person("");
//            Method m = p.getClass().getDeclaredMethod("setName", String.class);
//            m.setAccessible(true);
//            m.invoke(p, "mls");
//            System.out.println(p.getName());

//            Constructor cons1 = Integer.class.getConstructor(int.class);
//            Integer n1 = (Integer) cons1.newInstance(123);
//            System.out.println(n1);
//
//            Constructor cons2 = Integer.class.getConstructor(String.class);
//            Integer n2 = (Integer) cons2.newInstance("456");
//            System.out.println(n2);

//            Class i = Integer.class;
//            Class n = i.getSuperclass();
//            System.out.println(n);
//            Class o = n.getSuperclass();
//            System.out.println(o);
//            System.out.println(o.getSuperclass());

            Class s = Integer.class;
            Class[] is = s.getInterfaces();
            for (Class i: is) {
                System.out.println(i);

            }



        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Student extends Person {
    public int score;
    private int grade;

    public Student(String name) {
        super(name);
    }

    public int getScore() {
        return 99;
    }

    private int getGrade(int year) {
        return 11;
    }
}

class Person {
    private String name;
    public Person(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }

    private void setName(String name) {
        this.name = name;
    }
}
